package de.derhess.projects.pkr.controller
{
	
	import de.derhess.projects.pkr.models.LawyerInstanceCost;
	import de.derhess.projects.pkr.models.PKROutOfCourt;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.events.PropertyChangeEvent;
	
	import spark.formatters.NumberFormatter;
	
	
	/**
	 * released under MIT License (X11)
	 * http://www.opensource.org/licenses/mit-license.php
	 * 
	 * Class  ControllerPKROutOfCourt
	 * 
	 * @author Florian Weil [derhess.de, Deutschland]
	 * @see http://blog.derhess.de
	 */
	public class ControllerPKROutOfCourt
	{
		//--------------------------------------------------------------------------
		//
		//  Class variables
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		private var model:PKROutOfCourt;
		
		//--------------------------------------------------------------------------
		//
		//  Initialization
		//
		//--------------------------------------------------------------------------
		public function ControllerPKROutOfCourt(data:PKROutOfCourt)
		{
			model = data;
			
			
			this.model.businessFee.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
			this.model.riseFee.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
			this.model.conciliationFee.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
			this.model.expenses.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewVAT);
			
			this.model.vat.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE,handleNewSum);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		
		//--------------------------------------------------------------------------
		//
		//  Additional getters and setters
		//
		//--------------------------------------------------------------------------
		public function setLawyerFee(value:Number):void
		{
			this.model.businessFee.baseValue = value;
			this.model.riseFee.baseValue = value;
			this.model.conciliationFee.baseValue = value;
		}
		
		public function setNumberClients(value:int):void
		{
			this.model.riseFee.factor = Math.min((value - 1)*(0.3),2.0);
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		public function toString():String 
		{
			return "[ControllerPKROutOfCourt] : model: " + model.toString();	
		}
		//--------------------------------------------------------------------------
		//
		//  API
		//
		//--------------------------------------------------------------------------
		public function setStartValues(lawyerFee:Number = 0,numberOfClients:int = 1, vatValue:int = 19):void
		{
			this.model.businessFee.factor = 1.3;
			this.model.businessFee.baseValue = lawyerFee;
			setNumberClients(numberOfClients);
			this.model.riseFee.baseValue = lawyerFee;
			this.model.conciliationFee.factor = 0;
			this.model.conciliationFee.baseValue = lawyerFee;
			this.model.expenses.sum = 20;

			this.model.vat.percentage = vatValue;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Eventhandling
		//
		//--------------------------------------------------------------------------
		private function handleNewVAT(e:PropertyChangeEvent):void
		{

			var tSum:Number = model.businessFee.sum + model.riseFee.sum + model.conciliationFee.sum + model.expenses.sum;
			this.model.vat.baseValue = tSum;
		}
		
		
		private function handleNewSum(e:PropertyChangeEvent):void
		{
			this.model.sum = model.vat.sum + model.vat.baseValue;
		}
		//--------------------------------------------------------------------------
		//
		//  Broadcasting
		//
		//--------------------------------------------------------------------------
		
		
	}
}